<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" width="70vw" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back" v-if="type != 1">确定</el-button>
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-input v-model="search.keyword" placeholder="关键词搜索" clearable @keyup.enter="upsearch"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" @click="unsetSearch">重置</el-button>
                    </div>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" @row-dblclick="dbselectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                    <el-table-column type="selection" width="50" reserve-selection align="center" v-if="type != 1"></el-table-column>
                    <el-table-column type="index" label="序号" fixed align="center" width="60"></el-table-column>
                    <el-table-column label="样本" prop="title" width="250">
                        <template #default="scope">
                            <div>名称: {{scope.row.sample && scope.row.sample.title}}</div>
                            <div>编号: {{scope.row.sample && scope.row.sample.code}}</div>
                        </template>
                    </el-table-column>
					<el-table-column label="颜色" prop="sample.colors"></el-table-column>
                    <el-table-column label="图片" prop="image" align="center" width="100">
                        <template #default="scope">
                            <el-image v-if="scope.row.sample && scope.row.sample.image" style="width: 50px; height: 50px; border-radius: 5px;" :src="(scope.row.sample && scope.row.sample.image).split(',')[0]" :preview-src-list="(scope.row.sample && scope.row.sample.image).split(',')" preview-teleported="true" hide-on-click-modal></el-image>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" align="center" v-if="type == 1">
                        <template #default="scope">
                            <el-button type="primary" size="mini" @click="table_select(scope.row.sample && scope.row.sample)">选择</el-button>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
        </template>
    </el-dialog>
</template>

<script>

export default {
    props: {
        query: {type: Object, default: () => ({})},
    },
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '选择样本',
                edit: '选择样本',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
			apiObj: this.$API.basic.shopApply.list,
			selection: [],
			search: {
				keyword: null,
			},
            type: ""
        }
    },
    mounted() {
    },
    methods: {
        // 显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        setData(data) {
            this.type = data
        },
        // 选择事件
        selectionChange(selection) {
            this.selection = selection;
        },
        // 选中返回
        back() {
            this.$emit('success', this.selection, this.type)
            this.visible = false;
        },
        dbselectionChange(selection){
            this.$emit('success', selection.sample, this.type)
            this.visible = false;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        table_select(row){
            this.selection = row;
            this.back()
        },
    }
}
</script>

<style></style>
